package top.itaso.app.ui.wifi;

import android.net.wifi.ScanResult;
import android.os.Build;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.ArrayList;
import java.util.List;

import top.itaso.app.room.entity.UserEntity;
import top.itaso.app.room.entity.WifiScanResultEntity;
import top.itaso.app.room.repository.WifiScanResultRepository;

public class WifiScanResultViewModel extends ViewModel {

    private LiveData<List<WifiScanResultEntity>> _wifiScanResults = new MutableLiveData<>();

    public WifiScanResultViewModel() {
        _wifiScanResults = WifiScanResultRepository.getInstance().queryAll();
    }

    public LiveData<List<WifiScanResultEntity>> getWifiScanResults() {
        return _wifiScanResults;
    }

    public void saveWifiScanResult(ArrayList<ScanResult> scanWifiList) {
        ArrayList<WifiScanResultEntity> wifiScanResultEntities = new ArrayList<>();
        for (ScanResult scanResult : scanWifiList) {
            WifiScanResultEntity wifiScanResultEntity = new WifiScanResultEntity();
            wifiScanResultEntity.setBssid(scanResult.BSSID);
            wifiScanResultEntity.setSsid(scanResult.SSID);
            wifiScanResultEntity.setCapabilities(scanResult.capabilities);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                wifiScanResultEntity.setVenueName(scanResult.venueName + "");
                wifiScanResultEntity.setOperatorFriendlyName(scanResult.operatorFriendlyName + "");
                wifiScanResultEntity.setCenterFreq0(scanResult.centerFreq0);
                wifiScanResultEntity.setCenterFreq1(scanResult.centerFreq1);
                wifiScanResultEntity.setChannelWidth(scanResult.channelWidth);
                wifiScanResultEntity.setIs80211mcResponder(scanResult.is80211mcResponder());
                wifiScanResultEntity.setPasspointNetwork(scanResult.isPasspointNetwork());
            }
            wifiScanResultEntity.setFrequency(scanResult.frequency);
            wifiScanResultEntity.setLevel(scanResult.level);
            wifiScanResultEntity.setTimestamp(scanResult.timestamp);
            wifiScanResultEntity.setDescribeContents(scanResult.describeContents());
            wifiScanResultEntity.setWifiSsid(new WifiScanResultEntity.WifiSsid());

            wifiScanResultEntities.add(wifiScanResultEntity);
        }
//        WifiScanResultRepository.getInstance().insertAll(wifiScanResultEntities.get(0));
        WifiScanResultRepository.getInstance().insertAll(wifiScanResultEntities);
    }
}